package com.sihai.ehr.web.action.sign;

import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.Duty;
import com.sihai.ehr.model.biz.HelpDepartment;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.HelpDepartmentService;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.util.ArrayList;
import java.util.List;

/**
 * author: wxn
 * since: 13-4-30 上午11:50
 * 协助部门Action
 */

@Results({
        @Result(name = "main", location = "/WEB-INF/pages/sign/help_department_manage.jsp")
})
public class HelpDepartmentAction extends EHRAction{
    @BeanInject
    private HelpDepartmentService helpDepartmentService;
    @BeanInject
    private OrganizationService organizationService;

    private HelpDepartment department;
    private List<Organization> organizations;

    private boolean result;

    public int type;

    @Action("/public/help-department-main")
    @Override
    public String execute() throws Exception {
        return "main";
    }

    @Action(value = "/ajax/help-department-save", results = {@Result(name = "success", type = "json", params = {"root", "result"})})
    public String save() throws Exception {
        String[] helpDepartmentDatas = getParameterValues("help_department");
        if(helpDepartmentDatas == null || helpDepartmentDatas.length == 0){
            return SUCCESS;
        }
        List<HelpDepartment> list = new ArrayList<HelpDepartment>(helpDepartmentDatas.length);
        for(String temp : helpDepartmentDatas){
            String[] datas = StringUtils.split(temp, "-", 3);
            HelpDepartment department = new HelpDepartment();
            Organization o = new Organization();
            o.setId(NumberUtils.createLong(datas[0]));
            o.setFullName(datas[1]);
            department.setOrganization(o);
            department.setContent(datas[2]);
            list.add(department);
        }
        ServletActionContext.getRequest().getSession().setAttribute("choice_help_department_list", list);
        return SUCCESS;
    }

    public HelpDepartment getDepartment() {
        return department;
    }

    public void setDepartment(HelpDepartment department) {
        this.department = department;
    }

    public List<Organization> getOrganizations() {
        return organizations;
    }

    public boolean isResult() {
        return result;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }
}
